package com.demo.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

public class ThreadPoolUtil {
    static ExecutorService executorService;
    static int coreSize = 1;
    static int maxSize = 1;
    static int queSize = 1;
    static long keepAliveTime = 2;
    static TimeUnit timeUnit = TimeUnit.SECONDS;
    static BlockingQueue<Runnable> callableBlockingQueue = new LinkedBlockingQueue<>(queSize);
    static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("threadPool-%d").build();

    static {
        executorService
                = new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime, timeUnit,
                callableBlockingQueue, threadFactory);
    }

    public static Future<?> execute(Runnable runnable) {
        return executorService.submit(runnable);
    }

    public static <V> Future<V> submit(Callable<V> task) {

        return executorService.submit(task);
    }
}
